import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { StudentCourseGradeDTO } from 'src/app/project/model/icm/icm-student-academic/score-entry/student-course-grade.dto';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class StudentScoreService {
  private static URL = '/api/icm-student-grade';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询学生课程成绩信息
   * @param option 分页参数
   * @param academyName 学院名称
   * @param className 班级名称
   * @param studentName 学生姓名
   * @param courseName 课程名称
   * @param teacherId 授课老师id
   * @param year 所属年度
   * @param termType 所属学期
   * @param isCorrect 是否批改
   */
  findOnePage(
    option: QueryOptions,
    academyName?: string,
    className?: string,
    studentName?: string,
    courseName?: string,
    teacherId?: string,
    year?: string,
    termType?: string,
    isCorrect?: boolean,
  ): Observable<ApiPagedData<StudentCourseGradeDTO>> {
    const params = {};
    Object.assign(
      params,
      option,
      academyName ? { academyName } : {},
      className ? { className } : {},
      studentName ? { studentName } : {},
      courseName ? { courseName } : {},
      teacherId ? { teacherId } : {},
      year ? { year } : {},
      termType ? { termType } : {},
      isCorrect !== null ? { isCorrect } : {},
    );
    return this.http.get<ApiPagedData<StudentCourseGradeDTO>>(StudentScoreService.URL, params);
  }

  /**
   * 根据id查找学生课程成绩信息
   * @param id 学生课程成绩信息id
   */
  findById(id: string): Observable<StudentCourseGradeDTO> {
    return this.http.get<StudentCourseGradeDTO>(`${StudentScoreService.URL}/${id}`);
  }

  /**
   * 批改提交学生课程成绩信息
   * @param id 学生课程成绩id
   * @param studentCourseGradeDTO 学生课程成绩信息
   */
  complete(id: string, studentCourseGradeDTO: StudentCourseGradeDTO): Observable<StudentCourseGradeDTO> {
    return this.http.put<StudentCourseGradeDTO>(`${StudentScoreService.URL}/${id}`, studentCourseGradeDTO);
  }

  /**
   * 修改已批改的学生课程成绩信息
   * @param id 学生课程成绩id
   * @param studentCourseGradeDTO 学生课程成绩信息
   */
  update(id: string, studentCourseGradeDTO: StudentCourseGradeDTO): Observable<StudentCourseGradeDTO> {
    return this.http.put<StudentCourseGradeDTO>(`${StudentScoreService.URL}/update/${id}`, studentCourseGradeDTO);
  }

  /**
   * 根据id查找学生课程成绩信息
   * @param studentId 学生id
   */
  findAllByStudentId(studentId: string): Observable<StudentCourseGradeDTO> {
    return this.http.get<StudentCourseGradeDTO>(`${StudentScoreService.URL}/findAll/${studentId}`);
  }

  /**
   * 根据学年、学期模糊查找学生课程成绩
   * @param studentCode 学生学号
   * @param year 学年
   * @param termType 学期
   */
  search(studentCode?: string, year?: string, termType?: string): Observable<Array<StudentCourseGradeDTO>> {
    const params = {};
    Object.assign(params, studentCode ? { studentCode } : {}, year ? { year } : {}, termType ? { termType } : {});
    return this.http.get<Array<StudentCourseGradeDTO>>(`${StudentScoreService.URL}/search`, params);
  }
}
